package net.adetech.crj.service;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("submitTable")
public class SubmitTableService implements BaseService{

    @Value("${weixin_service.url}")
    private String weixinServiceUrl;

    @Override
    public Map<String, Object> invoke(Map<String, Object> params) {
        String idNumber = (String)params.get("idNumber");
        String clientNo = (String)params.get("clientNo");
        String idType = (String)params.get("idType");
        String realName = (String)params.get("realName");
        Map<String ,Object> resultMap = new HashMap<String, Object>();

        System.out.println("submitTable------------------->"+clientNo+":"+idNumber);

        try {
            List<NameValuePair> paramList = new ArrayList<NameValuePair>();
            paramList.add(new BasicNameValuePair("idNumber", idNumber));
            paramList.add(new BasicNameValuePair("clientNo", clientNo));
            paramList.add(new BasicNameValuePair("idType", idType));
            paramList.add(new BasicNameValuePair("realName", realName));
            HttpEntity reqEntity = new UrlEncodedFormEntity(paramList, "utf-8");
            HttpClient client = HttpClients.createDefault();
            HttpPost request = new HttpPost(weixinServiceUrl+"crj/submitTable");
            request.setEntity(reqEntity);
            //request.setHeader("Authorization","APPCODE f77530e7d1324c3bbd47c819aa33711c");
            HttpResponse response = client.execute(request);
            String responseString = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSONObject.parseObject(responseString);
            String result = jsonObject.getString("result");
            String queueNumber = jsonObject.getString("queueNumber");
            resultMap.put("result",result);
            resultMap.put("queueNumber",queueNumber);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}
